<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Comment Class
 *
 * @category	Home
 */
class CommentAction extends HomeAction
{

    protected $Comment;

	
    function _initialize()
	{
		parent::_initialize();
		$this->Comment = D('Comment');
	}


    //添加问题评论
    public function add()
    {

        $relatedID = intval($_REQUEST['rid']);
        $type = trim($_REQUEST['type']);

    	$captcha = require_captcha_when('post', $this->_user['user_group']);

        if (!$this->_userGroup['allow_comment'])
    	{
    		$this->error(L('no_perms_action'));
    	}

        switch ($type)
        {
        	case 'Q':
        		$Dao = D('Question');
        	    $message = L('question_not_existed');
        	break;
        	case 'A':
        	    $Dao = D('Answer');
        	    $message = L('answer_not_existed');
        	break;
        }

        $result = $Dao->find($relatedID);
        if (empty($result))
        {
            $this->error($message);
        }

    	if ($this->isPost())
    	{
    		//activity限制检查
        	if (exceed_activity_limit($this->_user['user_id'], $this->_user['user_group'], 'comment'))
        	{
        		$this->error(L('exceed_comment_limit'));
        	}

    		//内容长度检查
    		$minPostChars = $this->_global['min_post_chars'];
    		$maxPostChars = $this->_global['max_post_chars'];
    		if (strlen($_POST['details']) < $minPostChars)
    		{
    			$this->error(L('too_few_characters'));
    		}
    		if (strlen($_POST['details']) > $maxPostChars)
    		{
    			$this->error(L('too_much_characters'));
    		}
    		
    		//验证码验证
    		if ($captcha && md5($_POST['verify']) != $_SESSION['verify'])
    		{
    			$this->error(L('captcha_incorrect'));
    		}

    		//内容过滤
    		$wordCensor = $this->_global['word_censor_enable'];
        	if ($wordCensor)
        	{
        		$_POST['details'] = censor_text($_POST['details']);
        	}

        	//是否需要审核
        	if ($this->_userGroup['allow_direct_post'])
        	{
        		$message = L('comment_posted');
        		$isVisible = 1;
        	}
        	else
        	{
        		$message = L('comment_require_approval');
        		$isVisible = 0;
        	}

        	$data = array(
        		'category_id' => intval($result['category_id']),
        		'related_id'  => $relatedID,
        	    'type'        => $type,
        		'poster_id'   => $this->_user['user_id'],
        		'poster_name' => $this->_user['username'],
        		'poster_ip'   => get_client_ip(),
        		'details'     => trim($_POST['details']),
        		'create_time' => time(),
        		'markup_mode' => $this->_global['markup_mode'],
        		'is_visible'  => $isVisible,
        	);
        	$cmtID = $this->Comment->add($data);

        	//更新用户积分以及活动
    		$points = get_activity_points('comment');
    		$this->_User->logActivity($this->_user['user_id'], 'comment', $points, $cmtID);
    		$this->_User->updatePointCount($this->_user['user_id'], $points);

    		//更新用户评论计数
    		$this->_User->setInc('comment_count', 'user_id='.$this->_user['user_id'], 1);

    		//更新评论计数
    		$Dao->updateCommentCount($relatedID);

    		//答案新评论通知
    		if ($result['new_comment_alert'] && 'A' == $type)
    		{
    			import('ORG.Net.Http');
    			$requestUrl = get_site_domain(). U('Notification/newAnswerCommentAlert', array('cid' => $cmtID));
    			Http::async_request($requestUrl);
    		}

    		$jumpUrl = format_question_url($question['question_id'], $question['title']);
        	$this->assign('jumpUrl', $jumpUrl);
        	$this->success($message);
    	}
    	else
    	{
    	    $result['type'] = $type;
    	    $result['related_id'] = $relatedID;

    	    $this->assign('result', $result);
    	    $this->assign('captcha', $captcha);
    	    $html = $this->fetch();
    	    exit($html);
    	}
    }


    //编辑评论
    public function edit()
    {

    	$cmtID = intval($_REQUEST['cid']);
    	$comment = $this->Comment->find($cmtID);
    	if (empty($comment))
    	{
    		$this->error(L('comment_not_exsited'));
    	}
    	
    	$captcha = require_captcha_when('post', $this->_user['user_group']);

    	//用户权限检查
    	$isModerator = is_moderator($this->_user['user_id'], $this->_user['admin_group'], $comment['category_id']);
    	$allow = ($this->_userGroup['allow_edit_comment'] && $this->_user['user_id'] == $comment['poster_id']) || $isModerator && check_moderator_perms($this->_user['admin_group'],'allow_edit_comment');
    	if (!$allow)
    	{
    		$this->error(L('no_perms_action'));
    	}

    	if ($this->isPost())
    	{
			//内容长度检查
    		$minPostChars = $this->_global['min_post_chars'];
    		$maxPostChars = $this->_global['max_post_chars'];
    		if (strlen($_POST['details']) < $minPostChars)
    		{
    			$this->error(L('too_few_characters'));
    		}
    		if (strlen($_POST['details']) > $maxPostChars)
    		{
    			$this->error(L('too_much_characters'));
    		}
    		
    		//验证码检查
    		if ($captcha && md5($_POST['verify']) != $_SESSION['verify'])
    		{
    			$this->error(L('captcha_incorrect'));
    		}

	    	//内容过滤
	    	$wordCensor = $this->_global['word_censor_enable'];
	    	if ($wordCensor)
	    	{
	    		$_POST['details'] = censor_text($_POST['details']);
		    	if ('BANNED' == $_POST['details'])
		    	{
		    		$this->error(L('msg_contain_banned_words'));
		    	}
	    	}

	    	$data = array(
	    		'details' => trim($_POST['details']),
	    		'last_editor_id'   => $this->_user['user_id'],
	    		'last_editor_name' => $this->_user['username'],
	    		'last_edit_time'   => time(),
	    	);
	    	$where = array('comment_id' => $cmtID);
	    	$this->Comment->where($where)->save($data);
	    	$this->success(L('comment_edited'));
 		}
 		else
 		{
            $this->assign('comment', $comment);
            $this->assign('captcha', $captcha);
            $html = $this->fetch();
	    	exit($html);
 		}
    }


    //删除评论
    public function delete()
    {
    	$cmtID = intval($_GET['cid']);
    	$comment = $this->Comment->find($cmtID);
    	if (empty($comment))
    	{
    		$this->error(L('comment_not_existed'));
    	}

    	//用户权限检查
    	$isModerator = is_moderator($this->_user['user_id'], $this->_user['admin_group'], $comment['category_id']);
    	$allow = ($this->_userGroup['allow_delete_comment'] && $this->_user['user_id'] == $comment['poster_id']) || $isModerator && check_moderator_perms($this->_user['admin_group'],'allow_delete_comment');
    	if (!$allow)
    	{
    		$this->error(L('no_perms_action'));
    	}

    	//更新用户积分以及活动
		$points = get_activity_points('delete_comment');
		$this->_User->logActivity($comment['poster_id'], 'delete_comment', $points, $cmtID);
		$this->_User->updatePointCount($comment['poster_id'], $points);

		//删除评论
		$this->Comment->deleteCommentByID($cmtID);

		//更新评论计数
		switch ($comment['type'])
		{
			case 'Q':
			    $Dao = D('Question');
				break;
			case 'A':
			    $Dao = D('Answer');
				break;
		}
		$Dao->updateCommentCount($comment['related_id']);
        $this->success(L('comment_deleted'));
    }

}
// End Class


/* End of file CommentAction.class.php */
/* Location: ./App/Lib/Action/Home/CommentAction.class.php */